package com.weitu.archives.personArchives.controller;

import com.alibaba.fastjson.JSONArray;
import com.weitu.accessory.bean.AccessoryPackageInfo;
import com.weitu.accessory.service.AccessoryUtilService;
import com.weitu.archives.personArchives.entity.SafetyAssessmentRewardPunishmentLedger;
import com.weitu.archives.personArchives.entity.SafetyAsssessmentRewardPunishmentLedgerRelation;
import com.weitu.archives.personArchives.service.SafetyAssessmentRewardPunishmentLedgerService;
import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by smp on 2017/5/15.
 */
@Controller
@RequestMapping(value = "archives/personArchives/safetyAssessmentRewardPunishmentLedger")
public class SafetyAssessmentRewardPunishmentLedgerController extends BaseController{
    private static final String MODULE_BATH_PATH=BASE_JSP_PATH+"archives/personArchives/safetyAssessmentRewardPunishmentLedger";
    private final SafetyAssessmentRewardPunishmentLedgerService safetyAssessmentRewardPunishmentLedgerService;
    private final AccessoryUtilService accessoryUtilService;
    @Autowired
    public SafetyAssessmentRewardPunishmentLedgerController(SafetyAssessmentRewardPunishmentLedgerService safetyAssessmentRewardPunishmentLedgerService,AccessoryUtilService accessoryUtilService){
        this.safetyAssessmentRewardPunishmentLedgerService=safetyAssessmentRewardPunishmentLedgerService;
        this.accessoryUtilService=accessoryUtilService;
    }

    /**
     * 列表页
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(){
        return MODULE_BATH_PATH+"/list";
    }

    /**
     * 列表
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Object listAjax(HttpServletRequest request){
        Map<String,Object> paramMap=new HashMap<String,Object>();
        putParametersIntoMap(paramMap);
        int count=this.safetyAssessmentRewardPunishmentLedgerService.queryCount(paramMap);
        DataGridPagination dataGridPagination=DataGridPagination.getInstance(request,count);
        PageView pageView=dataGridPagination.getPageView();
        paramMap.put("pageView",pageView);
        List<SafetyAssessmentRewardPunishmentLedger> ledgerList=this.safetyAssessmentRewardPunishmentLedgerService.queryList(paramMap);
        return dataGridPagination.flushDataGridData(ledgerList);
    }

    /**
     * 跳到添加页面
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public ModelAndView add(){
        ModelAndView modelAndView=new ModelAndView();
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("addUserId",BaseController.getSessionUser().getId());
        map.put("addUserName",BaseController.getSessionUser().getRealName());
        map.put("addDeptId",BaseController.getSessionUser().getDepartment().getId());
        map.put("addDeptName",BaseController.getSessionUser().getDepartment().getName());
        map.put("addTime", DateTimeUtil.formatterDateTime("yyyy-MM-dd"));
        modelAndView.addObject("userMessage",map);
        modelAndView.setViewName(MODULE_BATH_PATH+"/add");
        return modelAndView;
    }

    /**
     * 信息
     * @return
     */
    @RequestMapping(value = "/information",method = RequestMethod.GET)
    public ModelAndView information(){
        ModelAndView modelAndView=new ModelAndView();
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("addUserId",BaseController.getSessionUser().getId());
        map.put("addUserName",BaseController.getSessionUser().getRealName());
        map.put("addDeptId",BaseController.getSessionUser().getDepartment().getId());
        map.put("addDeptName",BaseController.getSessionUser().getDepartment().getName());
        map.put("addTime", DateTimeUtil.formatterDateTime("yyyy-MM-dd"));
        modelAndView.addObject("userMessage",map);
        modelAndView.setViewName(MODULE_BATH_PATH+"/information");
        return modelAndView;
    }

    /**
     * 添加奖惩
     * @param punishmentLedger
     * @param rewardPunishments
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @SystemLog(module = "考核与奖惩台账",methods = "添加考核与奖惩台账")
    public AjaxResult addAjax(SafetyAssessmentRewardPunishmentLedger punishmentLedger,String rewardPunishments){
        punishmentLedger.setId(IDGenerator.getId());
        List<SafetyAsssessmentRewardPunishmentLedgerRelation> punishmentLedgerRelationList= JSONArray.parseArray(rewardPunishments,SafetyAsssessmentRewardPunishmentLedgerRelation.class);
        for(int i=0;i<punishmentLedgerRelationList.size();i++){
            punishmentLedgerRelationList.get(i).setId(IDGenerator.getId());
            punishmentLedgerRelationList.get(i).setRelationId(punishmentLedger.getId());
        }
        boolean flag=this.safetyAssessmentRewardPunishmentLedgerService.insertSafetyAssessmentRewardPunishmentLedgerAndRelation(punishmentLedger,punishmentLedgerRelationList);
        if(flag){
            return AjaxResult.success("添加成功");
        }else {
            return AjaxResult.fail("添加失败");
        }
    }

    /**
     * 编辑页面
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public ModelAndView edit(String id){
        SafetyAssessmentRewardPunishmentLedger rewardPunishmentLedger=this.safetyAssessmentRewardPunishmentLedgerService.queryRewardPunishLedgerById(id);
        List<AccessoryPackageInfo> accessoryList =  accessoryUtilService.queryAccessoryByBusinessId(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("rewardPunishmentLedger",rewardPunishmentLedger);
        modelAndView.addObject("accessoryList",accessoryList);
        modelAndView.addObject("uploadUserId",BaseController.getSessionUser().getId());
        modelAndView.setViewName(MODULE_BATH_PATH+"/edit");
        return modelAndView;
    }
    /**
     * 详情页面
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    public ModelAndView detail(String id){
        SafetyAssessmentRewardPunishmentLedger rewardPunishmentLedger=this.safetyAssessmentRewardPunishmentLedgerService.queryRewardPunishLedgerById(id);
        List<AccessoryPackageInfo> accessoryList =  accessoryUtilService.queryAccessoryByBusinessId(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("rewardPunishmentLedger",rewardPunishmentLedger);
        modelAndView.addObject("accessoryList",accessoryList);
        modelAndView.addObject("uploadUserId",BaseController.getSessionUser().getId());
        modelAndView.setViewName(MODULE_BATH_PATH+"/detail");
        return modelAndView;
    }
    /**
     * 关系
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/relation",method = RequestMethod.POST)
    public List<SafetyAsssessmentRewardPunishmentLedgerRelation> relationById(String id){
        List<SafetyAsssessmentRewardPunishmentLedgerRelation> relationList=this.safetyAssessmentRewardPunishmentLedgerService.queryListRelationById(id);
        return relationList;
    }

    /**
     * 修改
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @SystemLog(module = "考核与奖惩台账",methods = "编辑考核与奖惩台账")
    public AjaxResult editAjax(SafetyAssessmentRewardPunishmentLedger punishmentLedger,String rewardPunishments){
        punishmentLedger.setUpdateUserId(BaseController.getSessionUser().getId());
        punishmentLedger.setUpdateDeptId(BaseController.getSessionUser().getDepartment().getId());
        punishmentLedger.setUpdateTime(DateTimeUtil.formatterDateTime());
        List<SafetyAsssessmentRewardPunishmentLedgerRelation> punishmentLedgerRelationList= JSONArray.parseArray(rewardPunishments,SafetyAsssessmentRewardPunishmentLedgerRelation.class);
        for(int i=0;i<punishmentLedgerRelationList.size();i++){
            if(punishmentLedgerRelationList.get(i).getId()==null||punishmentLedgerRelationList.get(i).getId().equals("")){
                punishmentLedgerRelationList.get(i).setId(IDGenerator.getId());
            }
            punishmentLedgerRelationList.get(i).setRelationId(punishmentLedger.getId());
        }
        boolean flag=this.safetyAssessmentRewardPunishmentLedgerService.updateSafetyAssessmentRewardPunishmentLedgerAndRelation(punishmentLedger,punishmentLedgerRelationList);
        if(flag){
            return AjaxResult.success("编辑成功");
        }else {
            return AjaxResult.fail("编辑失败");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @SystemLog(module = "考核与奖惩台账",methods = "删除考核与奖惩台账")
    public AjaxResult delete(String id){
        int count=this.safetyAssessmentRewardPunishmentLedgerService.deleteSafetyAssessmentRewardPunishmentLedger(id);
        if(count==1){
            return AjaxResult.success("删除");
        }else{
            return AjaxResult.fail("删除");
        }
    }
    /**
     * 信息详情
     * @return
     */
    @RequestMapping(value = "/informationEdit",method = RequestMethod.GET)
    public ModelAndView informationEdit(String id) {
        ModelAndView modelAndView=new ModelAndView();
        SafetyAsssessmentRewardPunishmentLedgerRelation realation=this.safetyAssessmentRewardPunishmentLedgerService.selectRelationById(id);
        modelAndView.addObject("relation",realation);
        modelAndView.setViewName(MODULE_BATH_PATH+"/informationEdit");
        return modelAndView;
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/relationDetail",method = RequestMethod.GET)
    public ModelAndView relationDetail(String id){
        ModelAndView modelAndView=new ModelAndView();
        SafetyAsssessmentRewardPunishmentLedgerRelation realation=this.safetyAssessmentRewardPunishmentLedgerService.selectRelationById(id);
        modelAndView.addObject("relation",realation);
        modelAndView.setViewName(MODULE_BATH_PATH+"/relationDetail");
        return modelAndView;
    }
    /**
     * 先编辑
     * @param realation
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateRelation",method = RequestMethod.POST)
    @SystemLog(module = "考核与奖惩台账",methods = "编辑关联HSE奖惩详细信息")
    public AjaxResult updateRelation(SafetyAsssessmentRewardPunishmentLedgerRelation realation){
        int count=this.safetyAssessmentRewardPunishmentLedgerService.updateRelation(realation);
        if(count==1){
            return AjaxResult.success("编辑成功");
        }else{
            return AjaxResult.fail("编辑失败");
        }
    }
    /**
     * 插入关联
     * @param realation
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insertRelation",method = RequestMethod.POST)
    @SystemLog(module = "考核与奖惩台账",methods = "添加关联HSE奖惩详细信息")
    public AjaxResult insertRelation(SafetyAsssessmentRewardPunishmentLedgerRelation realation){
        AjaxResult ajaxResult=new AjaxResult();
        String id=IDGenerator.getId();
        realation.setId(id);
        int count=this.safetyAssessmentRewardPunishmentLedgerService.insertRelation(realation);
        if(count==1){
            ajaxResult.setData(id);
            ajaxResult.setMessage("添加成功");
            ajaxResult.setSuccess(true);
            return ajaxResult;
        }else{
            ajaxResult.setData(id);
            ajaxResult.setMessage("添加失败");
            ajaxResult.setSuccess(false);
            return ajaxResult;
        }
    }
}
